﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;

namespace DAL
{
    public class DBHelper
    {
        public static SqlConnection connection;
        static SqlDataAdapter adapter;
        static DataSet dataSet;
        public static String source = ConfigurationManager.ConnectionStrings["dbShopWhitSql"].ConnectionString;
        public static SqlConnection Connection
        {
            get
            {
                if (connection == null)
                {
                    connection = new SqlConnection(source);
                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Broken)
                {
                    connection.Close();
                    connection.Open();
                }
                return connection;
            }
        }

        public static DataTable Select(String sql)
        {
            try
            {
                adapter = new SqlDataAdapter(sql, Connection);
                dataSet = new DataSet();
                adapter.Fill(dataSet);
                return dataSet.Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
            
        }


        public static bool query(String sql)
        {
            try
            {
                SqlCommand sqlCommand = new SqlCommand(sql, Connection);
                int num = sqlCommand.ExecuteNonQuery();
                return num > 0 ? true : false;

            }catch(Exception ex)
            {
                throw ex;
            }
        }

        public static SqlDataAdapter GetSqlDataAdapter()
        {
            return adapter;
        }

        public static DataSet GetDataSet()
        {
            return dataSet;
        }
    }

}
